﻿using System;
using System.Web;

using StackExchange.Profiling;

namespace MVCBootstrap.Profiling {

	public class MiniProfilerStartupModule : IHttpModule {

		public void Init(HttpApplication context) {
			context.BeginRequest += (sender, e) => {
				var request = ((HttpApplication)sender).Request;
				//TODO: By default only local requests are profiled, optionally you can set it up
				//  so authenticated users are always profiled
				if (request.IsLocal) {
					MiniProfiler.Start();
				}
			};


			// TODO: You can control who sees the profiling information
			/*
			context.AuthenticateRequest += (sender, e) =>
			{
				if (!CurrentUserIsAllowedToSeeProfiler())
				{
					StackExchange.Profiling.MiniProfiler.Stop(discardResults: true);
				}
			};
			*/

			context.EndRequest += (sender, e) => {
				MiniProfiler.Stop();
			};
		}

		public void Dispose() { }
	}
}